package com.koron.ticket.work.workend.service;

import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.ticket.utils.StringUtils;
import com.koron.ticket.work.workend.bean.WorkEnd;
import com.koron.ticket.work.workend.bean.WorkEndQuery;
import com.koron.ticket.work.workend.mapper.WorkEndMapper;
import com.koron.ticket.work.workflow.service.WorkFlowService;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WorkEndService {

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    private static Date currentTime = new Date();

    @Autowired
    private WorkFlowService workFlowService;

    public void insert(SessionFactory factory, WorkEnd workEnd) {
        WorkEndMapper workEndMapper = factory.getMapper(WorkEndMapper.class);
        workEndMapper.insert(workEnd);
    }

    public void update(SessionFactory factory, WorkEnd workEnd) {
        WorkEndMapper workEndMapper = factory.getMapper(WorkEndMapper.class);
        workEndMapper.update(workEnd);
    }

    public void saveWithStatus(SessionFactory factory, WorkEnd workEnd, Integer status) {
        String id = workEnd.getId();
        if (StringUtils.isEmpty(id)) {
            workEnd.setId(CodeTools.getCode32());
            workEnd.setStatus(status);
            workEnd.setDeleteFlag(0);
            workEnd.setCreateUserId(getCurrUser().getId());
            workEnd.setCreateUserName(getCurrUser().getName());
            workEnd.setCreateTime(new Date());
            this.insert(factory, workEnd);
        } else {
            this.update(factory, workEnd);
        }
    }

    public void deleteById(SessionFactory factory, String id) {
        WorkEndMapper workEndMapper = factory.getMapper(WorkEndMapper.class);
        workEndMapper.deleteById(id);
    }

    public List<WorkEnd> getList(SessionFactory factory, WorkEndQuery workEndQuery) {
        WorkEndMapper workEndMapper = factory.getMapper(WorkEndMapper.class);
        return workEndMapper.getList(workEndQuery);
    }

    public List<WorkEnd> getId(SessionFactory factory, String ticketId) {
        WorkEndMapper workEndMapper = factory.getMapper(WorkEndMapper.class);
        WorkEndQuery workEndQuery = new WorkEndQuery();
        workEndQuery.setWorkTicketId(ticketId);
        return workEndMapper.getList(workEndQuery);
    }

    public List<WorkEnd> getList(SessionFactory factory, String tickId) {
        WorkEndQuery workEndQuery = new WorkEndQuery();
        workEndQuery.setWorkTicketId(tickId);
        return this.getList(factory, workEndQuery);
    }

    public WorkEnd getSingle(SessionFactory factory, WorkEndQuery workEndQuery) {
        WorkEndMapper workEndMapper = factory.getMapper(WorkEndMapper.class);
        return workEndMapper.getSingle(workEndQuery);
    }

    public void deleteByWorkTicketId(SessionFactory factory, String workTicketId) {
        WorkEndMapper workEndMapper = factory.getMapper(WorkEndMapper.class);
        workEndMapper.deleteByWorkTicketId(workTicketId);
    }


}
